From: =?utf-8?q?Aur=C3=A9lien_COUDERC?= <coucouf@debian.org>
Date: Thu, 2 Mar 2023 14:40:58 +0000
Subject: Fix %T filename pattern in fullscreen captures

Forwarded: https://phabricator.kde.org/D27969
Last-Update: 2022-03-02

The %T pattern can be used in file names to add the title of the window being
captured. In case of fullscreen capture when there is no title spectacles
removes this pattern, but also surrounding separators.

Unfortunately it is too aggressive in removing separators and will also eat
the % character of the closest following pattern in some cases. This breaks
patterns like %T_%Y-%M-%D_%H%m%S.

See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=953536
---
 src/ExportManager.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/ExportManager.cpp b/src/ExportManager.cpp
index 2500bbf..727e3b4 100644
--- a/src/ExportManager.cpp
+++ b/src/ExportManager.cpp
@@ -169,7 +169,9 @@ QString ExportManager::formatFilename(const QString &nameTemplate)
         title = mWindowTitle.replace(QLatin1Char('/'), QLatin1String("_")); // POSIX doesn't allow "/" in filenames
     } else {
         // Remove '%T' with separators around it
-        const auto wordSymbol = QStringLiteral(R"(\p{L}\p{M}\p{N})");
+        // Separators are anything that isn't a unicode letter \p{L}, mark \p{M}, number \p{N}
+        // or the % character to avoid eating into a nearby pattern.
+        const auto wordSymbol = QStringLiteral(R"(\p{L}\p{M}\p{N}%)");
         const auto separator = QStringLiteral("([^%1]+)").arg(wordSymbol);
         const auto re = QRegularExpression(QStringLiteral("(.*?)(%1%T|%T%1)(.*?)").arg(separator));
         baseName.replace(re, QStringLiteral(R"(\1\5)"));
